package calcolator;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame {
	double ilkDegisken;
	double ikinciDegisken;
	String operator;

	JTextField txtField = new JTextField("0");

	// rakamlara ait butonlar
	JButton btn1 = new JButton("1");
	JButton btn2 = new JButton("2");
	JButton btn3 = new JButton("3");
	JButton btn4 = new JButton("4");
	JButton btn5 = new JButton("5");
	JButton btn6 = new JButton("6");
	JButton btn7 = new JButton("7");
	JButton btn8 = new JButton("8");
	JButton btn9 = new JButton("9");
	JButton btn0 = new JButton("0");
	JButton btnNokta = new JButton(",");

	// islemlere ait butonlar
	JButton btnTopla = new JButton("+");
	JButton btnCikar = new JButton("-");
	JButton btnCarp = new JButton("x");
	JButton btnBol = new JButton("/");
	JButton btnEsit = new JButton("=");
	JButton btnSil = new JButton("<-");

	public Calculator(String name) {
		super(name);
		setSize(250, 400);
		setResizable(false);

		setLayout(new BorderLayout());

		txtField.setHorizontalAlignment(JTextField.RIGHT);
		txtField.setPreferredSize(new Dimension(100, 50));
		txtField.setEditable(false);
		txtField.setFont(new Font("Arial", Font.BOLD, 35));
		// txtField.setColumns(5);

		JPanel textFieldPanel = new JPanel(new BorderLayout(10, 0));
		textFieldPanel.add(BorderLayout.CENTER, txtField);
		textFieldPanel.add(BorderLayout.EAST, btnSil);
		JLabel boslabel = new JLabel();
		boslabel.setPreferredSize(new Dimension(100, 25));
		textFieldPanel.add(BorderLayout.SOUTH, boslabel);

		add(BorderLayout.NORTH, textFieldPanel);

		JPanel btnPanel = new JPanel(new GridLayout(4, 4, 10, 10));
		btnPanel.add(btn1);
		btnPanel.add(btn2);
		btnPanel.add(btn3);
		btnPanel.add(btnBol);
		btnPanel.add(btn4);
		btnPanel.add(btn5);
		btnPanel.add(btn6);
		btnPanel.add(btnCarp);
		btnPanel.add(btn7);
		btnPanel.add(btn8);
		btnPanel.add(btn9);
		btnPanel.add(btnCikar);
		btnPanel.add(btnNokta);
		btnPanel.add(btn0);
		btnPanel.add(btnEsit);
		btnPanel.add(btnTopla);

		add(BorderLayout.CENTER, btnPanel);

		btn1.addActionListener(new NumaraActionListener());
		btn2.addActionListener(new NumaraActionListener());
		btn3.addActionListener(new NumaraActionListener());
		btn4.addActionListener(new NumaraActionListener());
		btn5.addActionListener(new NumaraActionListener());
		btn6.addActionListener(new NumaraActionListener());
		btn7.addActionListener(new NumaraActionListener());
		btn8.addActionListener(new NumaraActionListener());
		btn9.addActionListener(new NumaraActionListener());
		btn0.addActionListener(new NumaraActionListener());

		btnTopla.addActionListener(new IslemActionListener());

	}

	class IslemActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JButton kaynakButton = (JButton) e.getSource();
			if (operator != null) {
				// islem yapilacak
				ikinciDegisken = Double.parseDouble(txtField.getText());

			} else {
				ilkDegisken = Double.parseDouble(txtField.getText());
				operator = kaynakButton.getText();
				txtField.setText("0");
			}
		}
	}

	class NumaraActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JButton kaynakButton = (JButton) e.getSource();
			if (txtField.getText().equals("0")) {
				txtField.setText(kaynakButton.getText());
			} else {
				txtField.setText(txtField.getText() + kaynakButton.getText());
			}
		}
	}

}
